<?php 
//all the class/ includes are managed in the includer file
include("include/includer.php");
//page specific includes
//RSS Feed Parser
require_once('magpierss72/rss_fetch.inc');

//get all the configuration variables (custom function in newzbook.php include)
getConfigVars();

//if the form was just posted back, let's use the form variables - otherwise the ones above will be used
if(isset($_GET['RSSFeed'])){
    $RSSFeed = str_replace(' ','+',$_GET['RSSFeed']);
}
if(isset($_GET['area'])){
    $area = $_GET['area'];
}
if(isset($_GET['matrix_category'])){
    $matrix_category = $_GET['matrix_category'];
}
if(isset($_GET['subcat'])){
    $subcat = $_GET['subcat'];
}
if(isset($_GET['english'])){
    $english = $_GET['english'];
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<HEAD>
    <?php standardHead("RSS Browser"); ?>
    <link type="text/css" href="style/smoothness/jquery-ui-1.7.2.custom.css" rel="stylesheet" />	
	<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
	<script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"></script>
    <script type='text/javascript'>
	    $(function() {
		$(".nzbreport").click(function(){ 
			$(this).children(".report_description").toggle("blind");
		});
	    });
    </script>
    <style>
	    .report_description {
		display:none;
		font-size:90%;
	    }
	    .reporttitle { 
		font-size:110%;
	    }
    </style>
</HEAD>

<BODY>
<?php internetSecurity(); ?>
<div style="visibility:hidden;display:none;" id="progress"><div id="progress_bg"></div><a name="loading" /><div id="progress_box"><img id="progress_image" style="padding-left:5px;padding-top:5px;" src="images/ajax-loader.gif" alt="loading...">Your request is being processed...</div></div>
<div class="headercontent">
  <?php headerContent(); ?>
</div>
<div class="maincontent">
<?php
if(!is_dir($config_NZBDropPath)) {
  echo "<div class='warningmessage'>The NZB Drop Path directory does not appear to exist. Check your <a href='configuration.php'>configuration</a></div>";
}

if($config_enablenzbget == "enable"){
  if(isset($config_NZBgetAppPath) && isset($config_NZBgetConfigPath)){
    if(!is_dir($config_NZBgetAppPath)) {
        echo "<div class='warningmessage'>The NZBGet Application directory does not appear to exist. Check your <a href='configuration.php'>configuration</a></div>";
        }
    if(!is_dir($config_NZBgetConfigPath)){
        echo "<div class='warningmessage'>The NZBGet Configuration directory does not appear to exist. Check your <a href='configuration.php'>configuration</a></div>";
        }
  }
}
?>
  <div class="block">
    <h2>RSS Feeds</h2>
    <form action="rssbrowser.php" method="get">
	<?php if($config_newzenabled == "enable"){ ?>
		<input type='radio' name='RSSFeed' value='NewzBin'>NewzBin (not enabled)</input>
	<?php } ?>
	<?php if($config_nzbmatrixenabled == "enable"){ ?>
		<input type='radio' name='RSSFeed' value='NZBMatrix' checked>NZBMatrix</input>
		<select name="subcat"><option value="">All Categories</option><option value="28">Anime: All</option><option value="20">Apps: Linux</option><option value="19">Apps: Mac</option><option value="21">Apps: Other</option><option value="18">Apps: PC</option><option value="55">Apps: Phone</option><option value="52">Apps: Portable</option><option value="53">Documentaries: HD</option><option value="9">Documentaries: STD</option><option value="16">Games: Dreamcast</option><option value="45">Games: DS</option><option value="46">Games: GameCube</option><option value="17">Games: Other</option><option value="10">Games: PC</option><option value="15">Games: PS1</option><option value="11">Games: PS2</option><option value="43">Games: PS3</option><option value="12">Games: PSP</option><option value="44">Games: Wii</option><option value="51">Games: Wii VC</option><option value="13">Games: Xbox</option><option value="14">Games: Xbox360</option><option value="56">Games: Xbox360 (Other)</option><option value="54">Movies: BRRip</option><option value="2">Movies: Divx/Xvid</option><option value="1">Movies: DVD</option><option value="50">Movies: HD (Image)</option><option value="42">Movies: HD (x264)</option><option value="4">Movies: Other</option><option value="3">Movies: SVCD/VCD</option><option value="48">Movies: WMV-HD</option><option value="24">Music: DVD</option><option value="23">Music: Lossless</option><option value="22">Music: MP3 Albums</option><option value="47">Music: MP3 Singles</option><option value="27">Music: Other</option><option value="25">Music: Video</option><option value="49">Other: Audio Books</option><option value="36">Other: E-Books</option><option value="33">Other: Emulation</option><option value="39">Other: Extra Pars/Fills</option><option value="37">Other: Images</option><option value="38">Other: Mobile Phone</option><option value="40">Other: Other</option><option value="34">Other: PPC/PDA</option><option value="26">Other: Radio</option><option value="6">TV: Divx/Xvid</option><option value="5">TV: DVD</option><option value="41">TV: HD</option><option value="8">TV: Other</option><option value="7">TV: Sport/Ent</option></select>
		<input type='hidden' name='english' value='off' />
		<input type='checkbox' name='english' <?php if(!isset($_GET["english"]) || $_GET["english"] == "on"){ echo "checked"; } ?>>English Only</input>
	<?php } ?>
	<br />
	<input type="submit" value="View" onclick="return loadSubmit()" />
    </form>
  </div>
<?php if($config_newzenabled == "enable" && ($config_newzuname == "" || $config_newzpw == "")){ die("<div class='warningmessage'>Username and/or password are not set. Please set the username/password on the <a href='index.php'>configuration page</a>.</div>"); } ?>
<?php
//get the search query variable
if(isset($_GET['RSSFeed'])){
  echo "<div class='block'>
    <h2>Results</h2>";
     echo "RSS Feed is set to: " . $RSSFeed;
     if($english == "on") { echo " (English Only)"; }
     echo "<br /><br />";
	  /* ---- NEWZBIN SEARCH ----- *\
	  \* ------------------------- */
	  if(isset($_GET['newzbin'])){
		 if($_GET['newzbin']=="on"){
		  //post the search to newzbin    
		  $url = 'http://'.$config_newzuname.':'.$config_newzpw.'@www.newzbin.com/search/query/?q=&area=&fpn=p&searchaction=Go&feed=rss&hauth=1';
		  $rss = fetch_rss( $url );
		  //parse out the fields and create the HTML for the page
		  foreach ($rss->items as $item) {      
			//create the HTML output
			echo "<div class='nzbreport'>";
			//Report Title
			preg_match("/\(Size\: (.+)\)/",$item['description'],$matches);
			echo "<div class='reporttitle'>".$item['title']."<div class='reporttitle_smaller'>(".$matches[1].")</div></div><br />";
			//Report Description - Attributes
			$tmpVar = explode("<li>",$item['description']);
			echo str_replace("</li>","",$tmpVar[2])."<br />";    
				//get the category from the attributes
				preg_match("/Category - (.*)/",$tmpVar[2],$newzbin_category);
				$newzbin_category = $newzbin_category[1];
			//Report Action Links
			echo "<div class='nzbactions'>";
			echo "<a href='".$item['guid']."'>View Report on Newzbin</a> | ";
			//if nzbget is enabled, lets send the user to the nzbget page
			if($config_enablenzbget == "enable"){ 
				echo "<a href='nzbget.php?reportid=".$item['report']['id']."&category=".$newzbin_category."' onclick='return loadSubmit()'>Download Now</a>"; 
			}
			else { 
				echo "<a href='index.php?action=reportactual&reportid=".$item['report']['id']."&category=".$newzbin_category."' onclick='return loadSubmit()'>Download Now</a>"; 
			}
			if($item['report']['nfo_link'] != "" || $item['report']['moreinfo'] != ""){
				echo " | ";
			}
			if($item['report']['nfo_link'] != ""){
				echo "(<a href='".$item['report']['nfo_link']."'>NFO</a>) ";
			}
			if($item['report']['moreinfo'] != ""){
				echo "(<a href='".$item['report']['moreinfo']."'>More Info</a>)";        
			}
			echo "</div>";
			//echo "Description: $rss_description <br />";
			echo "</div><br />";
		  }
		 }
		}

	  /* ---- NZBmatrix SEARCH ----- *\
	  \* --------------------------- */
	  if($RSSFeed == "NZBMatrix"){
		  //post the search to nzbmatrix   
		  $url = 'http://rss.nzbmatrix.com/rss.php?page=download&username='.$config_nzbmatrixuname.'&apikey='.$config_nzbmatrixapikey;
		  if(isset($subcat)){ $url = $url . "&subcat=" . $subcat; }
		  if(isset($english)){if($english == "on"){ $url = $url . "&english=1"; }}
		  $rss = fetch_rss( $url );
		
		  //first check for errors
		  if($rss == FALSE){ echo "<div class='warningmessage'>The request was not processed by NZBMatrix. Is the NZBMatrix RSS server available?</div>"; }
		  /*
		  if(substr($matrix_results, 0, 6) == 'error:'){
			  echo "<div class='warningmessage'>";
			  switch ($matrix_results) {
				case "error:invalid_login":
					echo "<div class='warningmessage'>NZBMatrix Error: There is a problem with the username you have provided.</div>";
					break;
				case "error:invalid_api":
					echo "<div class='warningmessage'>NZBMatrix Error: There is a problem with the API Key you have provided.</div>";
					break;
				case "error:invalid_nzbid":
					echo "<div class='warningmessage'>NZBMatrix Error: There is a problem with the NZBid supplied.</div>";
					break;
				case "error:vip_only":
					echo "<div class='warningmessage'>NZBMatrix Error: You need to be VIP or higher to access.</div>";
					break;
				case "error:disabled_account":
					echo "<div class='warningmessage'>NZBMatrix Error: User Account Disabled.</div>";
					break;
				case "error:no_nzb_found":
					echo "<div class='warningmessage'>NZBMatrix Error: No NZB found.</div>";
					break;
				case "error:no_scasearch":
					echo "<div class='warningmessage'>NZBMatrix Error: No search query.</div>";
					break;
				case "error:nothing_found":
					echo "<div class='warningmessage'>NZBMatrix Error: No Results Found.</div>";
					break;
				case "error:please_wait_60":
					echo "<div class='warningmessage'>You must wait 60 seconds between NZBMatrix searches. Try again later.</div>";
					break;
				default:
					echo $matrix_results;
					//Should continue below and split out the results
			  }echo "</div>";
		  }
		  */
		//print_r($rss);
		foreach ($rss->items as $item) {      
			//print_r($item);		
			echo "<div class='nzbreport'>";
			echo "<span class='reporttitle'>" . $item['title'] . "</span>";

			//convert the download link from the NZBMatrix RSS feed into a NewzBook formatted link
			//index.php?action=reportactual&reportid=REPORTID&indexer=nzbmatrix&reportName=REPORTNAME&category=CATEGORY
			
			//Grab the reportID from the link
			$tmp = preg_split("/[\&\?]+/", $item['link']);
			foreach($tmp as $tmp2){
				$tmp2 = preg_split("/[\=]+/", $tmp2); 
				if(isset($tmp2[1])){
					$item[$tmp2[0]] = $tmp2[1];
				}
			}
			//a little variable garbage cleanup
			unset($tmp, $tmp2);
			
			//Grab the reportName and category from the description
			$tmp = preg_split("/\<br\ \/\>/i", $item['description']);
			foreach($tmp as $tmp2){
				//remove the <b> tag
				$tmp2 = str_replace("<b>","",$tmp2);
				//split on ":</b> "
				$tmp2 = preg_split("/\:\<\/b\>\ /i",$tmp2);
				//set the items into the item array with the first part (bold word) from the description as the key and the actual item as the value
				if(isset($tmp2[1])){
					if($tmp2[0] == "Category"){ $tmp2[1] = str_replace(":"," >",$tmp2[1]); }
					$item[$tmp2[0]] = $tmp2[1];
				}
			}
			
			//now lets setup the download link
			$downloadLink = "<a href='index.php?action=reportactual&reportid=" . $item["id"] . "&indexer=nzbmatrix&reportName=" . $item["Name"] . "&category=" . $item["Category"] . "'>Download</a>";
			
			
			echo "<span class='reporttitle_smaller'>" . $downloadLink . "</span><br />";
			echo "<div class='report_description'>" . $item['description'] . "</div>";
			echo "</div>";
		}
		  
	      }
  echo "</div>";
}
?>
</div>
</BODY>
